package com.bw.finance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.finance.conf.ResponseResult;
import com.bw.finance.eneity.cms.EduWebsiteAd;
import com.bw.finance.eneity.locana.EbankLoanInfo;
import com.bw.finance.eneity.user.EbankBorrower;
import com.bw.finance.eneity.user.EbankInvestor;
import com.bw.finance.eneity.user.res.Loginuser;
import com.bw.finance.service.IEbankBorrowerService;
import com.bw.finance.utils.MD5Utils;
import com.bw.finance.utils.Photo.DanbaoImgUtils;
import com.bw.finance.utils.Photo.JiekuanImgUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/borrower")
public class EbankBorrowerController {
    @Autowired
    private IEbankBorrowerService borrowerService;

    private static final Logger log = LoggerFactory.getLogger(EbankBorrowerController.class);

    @PostMapping("/login")
    public ResponseResult login(@RequestBody Loginuser user,HttpServletResponse response){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result=borrowerService.login(user,response);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("用户登录异常"+e.toString());
        }
        return result;
    }

    @GetMapping("/userlogin")
    public ResponseResult login(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            System.out.println("aaaaaaa");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("用户登录异常"+e.toString());
        }
        return result;
    }

    /**
     * 获取借款人列表
     * @param pagenum
     * @param pagesize
     * @param ebankBorrower
     * @return
     */
    @PostMapping("/getBorrowerList/{pagenum}/{pagesize}")
    public ResponseResult getBorrowerList(@PathVariable int pagenum , @PathVariable int pagesize, @RequestBody EbankBorrower ebankBorrower){
        log.info("开始查询列表");
        return borrowerService.getBorrowerList(pagenum,pagesize,ebankBorrower);
    }

    /**
     * 拉黑借款人修改状态
     * @param id
     * @return
     */
    @PostMapping("/toBlackName/{id}")
    public ResponseResult toBlackName(@PathVariable Long id){
        log.info("开始将该"+id+"拉入黑名单");
        return borrowerService.toBlackName(id);
    }

    @PostMapping("/getBorrower/{pagenum}/{pagesize}")
    public ResponseResult getBorrower(@PathVariable int pagenum , @PathVariable int pagesize,@RequestBody EbankBorrower borrower){
        return borrowerService.getBorrowerList(pagenum,pagesize,borrower);
    }

    @PostMapping("/getBorrowerDetail/{id}")
    public ResponseResult getBorrowerDetail(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            EbankBorrower borrower = borrowerService.getById(id);
            result.setResult(borrower.getFullName());
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("拉入异常!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    @PostMapping("/uploadDanbaoImg")
    public ResponseResult uploadDanbaoImg(MultipartFile file){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String path = DanbaoImgUtils.upload(file);

            result.setResult(path);
            result.setMessage("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("上传失败!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    @PostMapping("/uploadJiekuanImg")
    public ResponseResult uploadJiekuanImg(MultipartFile file){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            String path = JiekuanImgUtils.upload(file);
            result.setResult(path);
            result.setMessage("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("上传失败!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    @PostMapping("/peocheck")
    public ResponseResult peocheck(@RequestBody EbankBorrower borrower){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("身份证认证开始");
        try {
            EbankBorrower ebankBorrower = borrowerService.getById(borrower.getId());
            ebankBorrower.setIdNum(borrower.getIdNum());
            borrowerService.saveOrUpdate(ebankBorrower);
            result.setMessage("认证成功");
        }catch (Exception e){
            log.info("身份证认证失败!"+e.toString());
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("身份证认证结束");
        return result;
    }

    @PostMapping("/bankcheck")
    public ResponseResult bankcheck(@RequestBody EbankBorrower borrower){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("银行账户认证开始");
        try {
            EbankBorrower ebankBorrower = borrowerService.getById(borrower.getId());
            ebankBorrower.setBank(borrower.getBank());
            ebankBorrower.setAccount(borrower.getAccount());
            ebankBorrower.setTokenPwd(borrower.getTokenPwd());
            borrowerService.saveOrUpdate(ebankBorrower);
            result.setMessage("认证成功");
        }catch (Exception e){
            log.info("银行账户认证失败!"+e.toString());
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("银行账户认证结束");
        return result;
    }

    @PostMapping("/emailcheck")
    public ResponseResult emailcheck(@RequestBody EbankBorrower borrower){
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("邮箱账户认证开始");
        try {
            result=borrowerService.emailcheck(borrower);
        }catch (Exception e){
            log.info("邮箱账户认证失败!"+e.toString());
            result.setMessage("认证失败!"+e.toString());
            result.setSuccess(false);
        }
        log.info("邮箱账户认证结束");
        return result;
    }

    @GetMapping("/findone/{id}")
    public ResponseResult findone(@PathVariable Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("个人信息查询开始");
        try {
            EbankBorrower borrower = borrowerService.getById(id);
            result.setResult(borrower);
        } catch (Exception e) {
            log.info("个人信息查询失败!" + e.toString());
            result.setMessage("查询失败!" + e.toString());
            result.setSuccess(false);
        }
        log.info("个人信息查询结束");
        return result;
    }

    @PostMapping("/IsGoOk/{id}")
    public ResponseResult IsGoOk(@PathVariable("id") String id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            log.info("开始审核");
            EbankBorrower borrower = borrowerService.getById(id);
            borrower.setStatus(1);
            borrowerService.saveOrUpdate(borrower);
            log.info("审核结束");
            result.setMessage("审核通过");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("审核通过异常!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    @PostMapping("/TijiaoScore/{id}")
    public ResponseResult TijiaoScore(@PathVariable("id") String id,@RequestBody EbankBorrower borrowerMessage) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            log.info("填写信用分开始");
            EbankBorrower borrower = borrowerService.getById(id);
            borrower.setCreditScore(borrowerMessage.getCreditScore());
            borrowerService.saveOrUpdate(borrower);
            log.info("填写信用分结束");
            result.setMessage("填写信用分成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("审核通过异常!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    @GetMapping("/download/{path}")
    public ResponseResult download(@PathVariable String path,HttpServletResponse response){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            DanbaoImgUtils.download(path,response);
            result.setMessage("下载成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("下载失败!");
            result.setSuccess(false);
            return result;
        }
        return result;
    }

    //借款时的服务调用
    @PostMapping("/getborrowone")
    public EbankBorrower getborrowone(@RequestBody EbankLoanInfo ebankLoanInfo){
        return borrowerService.getborrowone(ebankLoanInfo);
    }

    @GetMapping("/finduser/{id}/{type}")
    public ResponseResult finduser(@PathVariable Long id,@PathVariable Integer type) {
        ResponseResult result = ResponseResult.SUCCESS();
        log.info("个人信息查询开始");
        try {
            result=borrowerService.finduser(id,type);
        } catch (Exception e) {
            log.info("个人信息查询失败!" + e.toString());
            result.setMessage("查询失败!" + e.toString());
            result.setSuccess(false);
        }
        log.info("个人信息查询结束");
        return result;
    }
}
